package com.cindata

import com.cindata.dictionary.*
import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Transactional
@Secured(['isAuthenticated()'])
class AccountProjectBuildingController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AccountProjectBuilding.list(params), model:[accountProjectBuildingCount: AccountProjectBuilding.count()]
    }

    def show(AccountProjectBuilding accountProjectBuilding) {
        respond accountProjectBuilding
    }

    def projectBuildingShow(AccountProjectBuilding accountProjectBuilding) {

    }

    def create() 
    {
        params['targetUri'] = request.getHeader("referer")
        respond new AccountProjectBuilding(params)
    }

    @Transactional
    def save(AccountProjectBuilding accountProjectBuilding) {
        if (accountProjectBuilding == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountProjectBuilding.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountProjectBuilding.errors, view:'create'
            return
        }

        accountProjectBuilding.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'accountProjectBuilding.label', default: 'AccountProjectBuilding'), accountProjectBuilding.id])
                redirect url: params['targetUri']
            }
            '*' { respond accountProjectBuilding, [status: CREATED] }
        }
    }

    def edit(AccountProjectBuilding accountProjectBuilding) 
    {
        params['targetUri'] = request.getHeader("referer")
        respond accountProjectBuilding
    }

    @Transactional
    def update(AccountProjectBuilding accountProjectBuilding) {
        if (accountProjectBuilding == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountProjectBuilding.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountProjectBuilding.errors, view:'edit'
            return
        }

        accountProjectBuilding.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountProjectBuilding.label', default: 'AccountProjectBuilding'), accountProjectBuilding.id])
                redirect url: params['targetUri']
            }
            '*'{ respond accountProjectBuilding, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountProjectBuilding accountProjectBuilding) {

        if (accountProjectBuilding == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        accountProjectBuilding.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'accountProjectBuilding.label', default: 'AccountProjectBuilding'), accountProjectBuilding.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountProjectBuilding.label', default: 'AccountProjectBuilding'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
